<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="./js/animate1.js"></script>
  <style>
    * {
      padding: 0;
      margin: 0;
    }

    .wrapAll {
      padding: 10px;
    }

    .buttons button+button {
      margin-left: 10px;
    }

    .wrap {
      width: 1100px;
      height: 100px;
      border: 1px solid #333;
      margin-top: 10px;
      position: relative;
    }

    .sub {
      width: 100px;
      height: 100px;
      background-color: #6ff;
      position: absolute;
      left: 00px;
    }
  </style>
</head>

<body>
  <div class="wrapAll">
    <div class="buttons">
      <button class="start">开始</button>
      <button class="pause">暂停</button>
      <button class="back">返回</button>
    </div>
    <div class="wrap">
      <div class="sub"></div>
    </div>
  </div>
  <div class="wrapAll">
    <div class="buttons">
      <button class="start">开始</button>
      <button class="pause">暂停</button>
      <button class="back">返回</button>
    </div>
    <div class="wrap">
      <div class="sub"></div>
    </div>
  </div>
  <div class="wrapAll">
    <div class="buttons">
      <button class="start">开始</button>
      <button class="pause">暂停</button>
      <button class="back">返回</button>
    </div>
    <div class="wrap">
      <div class="sub"></div>
    </div>
  </div>
  <div class="wrapAll">
    <div class="buttons">
      <button class="start">开始</button>
      <button class="pause">暂停</button>
      <button class="back">返回</button>
    </div>
    <div class="wrap">
      <div class="sub"></div>
    </div>
  </div>
</body>
<script>
  var startList = document.getElementsByClassName("start");
  var pauseList = document.getElementsByClassName("pause");
  var backList = document.getElementsByClassName("back");
  var subList = document.getElementsByClassName("sub");

  // 问题?
  //  多个计时器,共有一个timer 记录计时器的编号,  每次启动下一个会清除上一个 , => 始终只有最后一个在跑  
  // 解决方法: 每一个计时器 都有一个对应的变量 保存计时器的标号  

  document.onclick = function (e) {
    var e = e || window.event;
    var target = e.target || e.srcElement;

    if (target.className == "start") {
      var list = [...startList];
      var index = list.indexOf(target);
      // console.log(target,index,subList[index]);

      animate(subList[index], "left", 1000, "fast", function () {
        console.log("到达终点");
      })
    } else if (target.className == "pause") {
      clearInterval(timer);
    } else if (target.className == "back") {
      var list = [...backList];
      var index = list.indexOf(target);
      // console.log(target,index,subList[index]);

      animate(subList[index], "left", 0, "slow", function () {
        console.log("到达终点");
      })
    }

  }

</script>

</html>